<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bug" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="add('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data,node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon v-if="data.children.length===0" icon-class="people" />
                <svg-icon v-else :icon-class="node.expanded?'jianhao':'add'" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="Edit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-show="data.children.length===0" @click.native="isDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog :title="isEdit?'编辑':'添加'" :visible.sync="showDialog" @close="close">
      <AddorEdit v-if="showDialog" :id="id" :is-edit="isEdit" :rule-list="ruleList" @success="close" @quxiao="showDialog=false" />
    </el-dialog>
  </div>
</template>

<script>
import AddorEdit from './dep01.vue'
// 获取数据 和 删除
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'
export default {
  name: '',
  components: {
    AddorEdit
  },
  props: {},
  data() {
    return {
      showDialog: false, // 是否显示弹层
      list: [],
      id: '',
      isEdit: false,
      ruleList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 关闭弹框
    close() {
      this.showDialog = false
      this.getList()
    },
    // 获取数据结构
    async getList() {
      try {
        const res = await getDepartments()
        // console.log(res.data.depts, 11)
        res.data.depts.shift()
        this.ruleList = res.data.depts
        this.list = tranListToTreeData(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    // 添加事件
    add(id) {
      this.showDialog = true
      this.isEdit = false
      this.id = id
    },
    // 编辑
    Edit(id) {
      this.showDialog = true
      this.isEdit = true
      this.id = id
    },
    // 删除
    async del(id) {
      try {
        await delDepartment(id)
        this.getList()
        this.$message({ type: 'success', message: '删除成功' })
      } catch (err) {
        this.$message({ type: 'error', message: err })
      }
    },
    // 删除确认框
    isDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.del(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-tree-node__expand-icon{
  display: none;
}
</style>
